package com.bootdo.staticmask.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.staticmask.domain.MaskFileDO;
import com.bootdo.staticmask.service.MaskFileService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

@Controller
@RequestMapping("/staticmask/maskFile")
public class MaskFileController {

	private final MaskFileService maskFileService;

	@Autowired
	public MaskFileController(MaskFileService maskFileService) {
		this.maskFileService = maskFileService;
	}

	@GetMapping()
	@RequiresPermissions("staticmask:maskFile:maskFile")
	String MaskFile(){
	    return "staticmask/maskFile/maskFile";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("staticmask:maskFile:maskFile")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MaskFileDO> maskFileList = maskFileService.list(query);
		int total = maskFileService.count(query);
		PageUtils pageUtils = new PageUtils(maskFileList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("staticmask:maskFile:add")
	String add(){
	    return "staticmask/maskFile/add";
	}

	@GetMapping("/edit/{fileId}")
	@RequiresPermissions("staticmask:maskFile:edit")
	String edit(@PathVariable("fileId") Integer fileId,Model model){
		MaskFileDO maskFile = maskFileService.get(fileId);
		switch (maskFile.getTextQualifier()){
			case "\"":maskFile.setTextQualifier("double");break;
			case "'":maskFile.setTextQualifier("single");break;
			case " ":maskFile.setTextQualifier("none");break;
			default:maskFile.setTextQualifier("none");break;
		}

		model.addAttribute("maskFile", maskFile);
	    return "staticmask/maskFile/edit";
	}

	/**
	 * 验证文件是否存在
	 * @param filePath 文件路径
	 * @return R ok是成果，否则失败
	 */
	@ResponseBody
	@GetMapping("/check")
	public boolean check(String filePath){
		File file = new File(filePath);
		return file.exists();
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("staticmask:maskFile:add")
	public R save(MaskFileDO maskFile){
		maskFile.setCreatetime(new Date());
		if(maskFileService.save(maskFile)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("staticmask:maskFile:edit")
	public R update(MaskFileDO maskFile){
		maskFileService.update(maskFile);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskFile:remove")
	public R remove( Integer fileId){
		if(maskFileService.remove(fileId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskFile:remove")
	public R remove(@RequestParam("ids[]") Integer[] fileIds){
		maskFileService.batchRemove(fileIds);
		return R.ok();
	}
	
}
